package org.NooLab.itexx.co.targets;

import java.io.IOException;

import org.NooLab.itexx.ITexx;
import org.NooLab.itexx.ITexxFactory;
import org.NooLab.itexx.app.up.TxStartup;
import org.NooLab.itexx.co.CoWorker;
import org.NooLab.itexx.co.CoWorkers;
import org.NooLab.itexx.co.ITexxModuleAppIntf;
import org.NooLab.utilities.net.GUID;


/**
 * 
 * 
 * the "Co-Classes" for configuration also create the initial startup-seed-info, 
 * if this does not exist yet.
 * 
 * 
 *
 */
public class CoListenerServer implements ITexxModuleAppIntf{

	ITexxFactory itxFactory ;
	
	CoWorker processDef ;
	CoWorkers coWorkers;
	ITexx itexx ;
/*
 
-prj: docoserv  
-homedir: D:/data/iTexx/DocumentServer/DocumentServer 
-cfg: docoserv-config.xml  
-notify:8988,8989
	 
*/
	
	public CoListenerServer( ITexxFactory factory, ITexx itx ){
		
		itxFactory = factory ;
		
		itexx = itx ;
		coWorkers = itexx.getCoWorkersContainer();
	}
	
	@Override
	public void configure(int dedicatedPort, int maxNumberOfConcurrents) throws IOException{
		
		String path ;
		
		processDef = coWorkers.getCoWorker(itexx) ;
		
		processDef.setJarFilename("NooLabDocumentObserver.jar") ;
		processDef.setMainClass("org.NooLab.docserver.DocumentObserverApp") ;
		
		int ccn = coWorkers.GetConcurrentCountOf( processDef );
		if (ccn>maxNumberOfConcurrents){
			// in particular, we will not add this process definition to 
			// the items list in CoWorkers, and also we will not create an
			// instance of JavaProcessBuilder (jvm)
			return;
		}

		
		processDef.setJarFilePath("/bin"); // relative to system root   
		processDef.setExtLibrariesPath("/bin/lib") ;
		                                path = coWorkers.fileutil.createpath(itxFactory.getSystemRootDir(),"/bin/lib"); 
		processDef.setExtLibrariesPath( path ) ;
		
		                             path = coWorkers.fileutil.createpath(itxFactory.getSystemRootDir(),"app/DocumentServer");
		processDef.setAppWorkingDir( path) ; // relative to system root 
		
		processDef.addArgument( "-prj:itx");  
		
		// TODO make this dynamic
								path = TxStartup.getProjectBasePath();
								path = coWorkers.fileutil.createpath(itxFactory.getSystemRootDir(), "app/DocumentServer");
		processDef.addArgument( "-homedir: "+path); 
		processDef.addArgument( "-cfg: docoserv-config.xml");
		processDef.addArgument( "-context:itexx"); 
		
        						String instanceGuid = GUID.randomvalue() ;
		processDef.setInstanceGuid(instanceGuid);
		processDef.addArgument( "-waitfor:"+instanceGuid); 
		processDef.addArgument( "-name:_listener_"); // we use a small decoration

		processDef.addArgument( "-loglevel:2"); 		
		processDef.setName(this.getClass().getSimpleName());
		
		processDef.create() ;

		coWorkers.add(processDef) ;
	}
	
	@Override
	public CoWorker getProcessDef() {
		return processDef;
	}

	@Override
	public void launch() throws Exception{
		processDef.launch() ;
	}
	
}
